<?php
    include "../class/address.class.php";
    include "../function/functionPHP.php";
    $mysqli=  connect();
    
    $addressClass=new address();
    
    $keyword=$_POST["keyword"];
    
    if($keyword!=""){
        $query="
            select  *
            from    thai_subdistrict
            where   subDistrictName like '%".$keyword."%' or
                    districtID in (
                        select  districtID
                        from    thai_district
                        where   districtName like '%".$keyword."%'
                    ) or
                    provinceID in (
                        select  provinceID
                        from    thai_province
                        where   provinceName like '%".$keyword."%'
                    )
            ";
        $result=$mysqli->query($query);
        if($result->num_rows>0){
            while($row=$result->fetch_array(MYSQLI_ASSOC)){
                $district=$addressClass->getDistrict($row["districtID"]);
                $province=$addressClass->getProvince($row["provinceID"]);
                ?>
<div class="hover"
    style="
        font-size: 80%;
        padding-bottom: 10px;
        margin-top: 10px;
        float: bottom;
        width: 100%;
        border-bottom: 1px dashed #aaa;
        cursor: pointer;
    "
    onclick="
        var subDistrictID = '<?=$row["subDistrictID"]?>';
        var subDistrictName = '<?=$row["subDistrictName"]?>';
        var districtID = '<?=$row["districtID"]?>';
        var districtName = '<?=$district["districtName"]?>';
        var provinceID = '<?=$row["provinceID"]?>';
        var provinceName = '<?=$province["provinceName"]?>';
        
        setAddress(subDistrictID,subDistrictName,districtID,districtName,provinceID,provinceName);
    "> 
    ตำบล <?=$row["subDistrictName"]?> &nbsp;&nbsp;
    อำเภอ <?=$district["districtName"]?> &nbsp;&nbsp;
    <?=$province["provinceName"]?> &nbsp;&nbsp;
</div>
                <?php
            }
        }else{
            echo "ไม่พบข้อมูล";
        }
    }else{
        echo "กรุณากรอกคำค้น";
    }